package com.express.gateway.manage.entity;

import io.vertx.core.json.JsonObject;

import java.util.Optional;

/**
 * API返回结果状态码
 *
 * @author flower
 */
public class ResultStatus {
    private String code;// 状态码
    private String msg;// 错误信息
    private String describe;// 描述

    /**
     * 将对象装换为JsonObject
     *
     * @return
     */
    public JsonObject toJson() {
        JsonObject json = new JsonObject();
        Optional.ofNullable(this.msg).ifPresent(m -> json.put("msg", m));
        Optional.ofNullable(this.code).ifPresent(c -> json.put("code", c));
        Optional.ofNullable(this.describe).ifPresent(d -> json.put("describe", d));
        return json;
    }

    /**
     * 通过JsonObject实例化一个对象
     *
     * @param obj
     * @return
     */
    public static ResultStatus fromJson(JsonObject obj) {
        if (obj == null) {
            return null;
        }
        ResultStatus option = new ResultStatus();
        if (obj.getValue("code") instanceof String) {
            option.setCode(obj.getString("code"));
        }
        if (obj.getValue("msg") instanceof String) {
            option.setMsg(obj.getString("msg"));
        }
        if (obj.getValue("describe") instanceof String) {
            option.setDescribe(obj.getString("describe"));
        }
        return option;
    }

    public ResultStatus() {
        super();
    }

    public ResultStatus(String code, String msg, String describe) {
        super();
        this.code = code;
        this.msg = msg;
        this.describe = describe;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    @Override
    public String toString() {
        return "ExpApiResultStatus [code=" + code + ", msg=" + msg + ", describe=" + describe + "]";
    }

}
